package com.sea.physical.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.physical.entity.Freight;
import com.sea.physical.entity.FreightRecord;
import com.sea.physical.service.*;
import com.sea.supply.entity.ProductPriceRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/physical/freight/record")
public class FreightRecordController {

    @Autowired
    private FreightService freightService;
    @Autowired
    private FreightRecordService freightRecordService;


    /**
     * 管理主页面
     *getPhysicalInfo
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("id",id);
        return "/sea/physical/freight/record";
    }

    @RequestMapping("/test")
    public String test(){
        List<Freight> byCnd = freightService.findByCnd(null);
        for(Freight freight:byCnd){

            FreightRecord record=new FreightRecord();
            record.setFreightId(freight.getId());
            record.setFreightTotal(freight.getFreightTotal());
            freightRecordService.save(record);
        }

        return "";
    }



    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByFreightId")
    public AjaxTableReturn findByFreightId(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                         @RequestParam(value = "offset", defaultValue = "0") int start,
                                         Integer freightId
    ) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<FreightRecord> list = freightRecordService.findByFreightId(freightId);
        PageInfo<FreightRecord> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 根据pid 查询历史价格
     * @param freightId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getHistoryPrice")
    public AjaxReturn getHistoryPrice(@RequestParam("freightId") Integer freightId){
        AjaxReturn result = Ajax.fail().setMsg("查询失败！");
        List<FreightRecord> list = freightRecordService.findByFreightId(freightId);

        if(list.size() > 0){
            result.setOk(true).setData(list);
        }else{
            result.setMsg("暂未查询到历史价格!");
        }
        return result;
    }



}
